home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_11_09
/
1109107a
< prev
next >
Wrap
Text File
|
1993-07-16
|
1KB
|
56 lines
#include <stdio.h>
#include <string.h>
//
// String class with overloaded cat function
//
class String
{
public:
String() { ... }
String(const char *s) { ... }
String(const String &s) { ... }
~String() { delete [] str; }
// the [] were missing
size_t length() { return len; }
const char *text() { return str; }
void cat(char c);
void cat(const char *s);
void cat(const String &s);
char &sub(size_t i) { ... }
private:
size_t len;
char *str;
};
// ...
//
// append a nul-terminated string to a String
//
void String::cat(const char *s)
{
size_t n = len + strlen(s) + 1;
char *p = strcpy(new char[n], str);
strcat(p, s);
delete [] str; // the [] were missing
str = p;
len = n; // this line was missing
}
//
// append a String to a String
//
void String::cat(const String &s)
{
size_t n = len + s.len + 1;
char *p = strcpy(new char[n], str);
strcat(p, s.str);
delete [] str; // the [] were missing
str = p;
len = n; // this line was missing
}
// ...